import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserProfile, getUserDetailByID } from '@/api/user'
import { resetRouter } from '@/router'
const state = {
  token: getToken() || '',
  userInfo: {}
}

const mutations = {
  setToken(state, value) {
    state.token = value
    setToken(value)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setUser(state, obj) {
    state.userInfo = obj
  },
  removeUser(state) {
    state.userInfo = {}
  }
}

const actions = {
  logout(content) {
    content.commit('removeToken')
    content.commit('removeUser')
    // 重置路由
    resetRouter()
  },
  async setLogin(content, obj) {
    const res = await login(obj)
    // console.log('content', content)
    content.commit('setToken', res.data)
  },
  async getUser(content) {
    const res = await getUserProfile()
    const UserInfo = await getUserDetailByID(res.data.userId)
    content.commit('setUser', { ...res.data, ...UserInfo.data })
    return res.data.roles.menus
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

